package org.yunai.swjg.client;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.yunai.swjg.client.mina.ClientIoHandler;
import org.yunai.swjg.server.core.message.codec.GameMessageCodecFactory;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 假装是客户端
 * User: yunai
 * Date: 13-3-15
 * Time: 上午10:41
 */
public class Client {

    private NioSocketConnector connector;

    private void init() {
        connector = new NioSocketConnector();
        // io handler
        connector.setHandler(new ClientIoHandler());
        // filterChain
        Map<String, IoFilter> filters = new LinkedHashMap<String, IoFilter>();
        filters.put("protocolCodecFilter", new ProtocolCodecFilter(new GameMessageCodecFactory()));
        DefaultIoFilterChainBuilder filterChainBuilder = new DefaultIoFilterChainBuilder();
        filterChainBuilder.setFilters(filters);
        connector.setFilterChainBuilder(filterChainBuilder);
    }

    private void start() throws IOException {
        // 初始化
        init();
        //
        connector.connect(new InetSocketAddress(8080));
        System.out.println("connector connect success!");
    }

    public static void main(String[] args) throws IOException {
        new Client().start();
    }
}
